home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-19 / gplot386.zip / DOC2MS.C < prev    next >
C/C++ Source or Header  |  1992-06-06  |  5KB  |  247 lines

  1. #ifndef lint
  2. static char *RCSid = "$Id: doc2ms.c,v 3.26 1992/03/25 04:53:29 woo Exp woo $";
  3. #endif
  4.  
  5. /*
  6.  * doc2ms.c  -- program to convert Gnuplot .DOC format to *roff -ms document
  7.  * From hlp2ms by Thomas Williams 
  8.  *
  9.  * Modified by Russell Lang, 2nd October 1989
  10.  * to make vms help level 1 and 2 create the same ms section level.
  11.  *
  12.  * Modified to become doc2ms by David Kotz (David.Kotz@Dartmouth.edu) 12/89
  13.  * Added table and backquote support.
  14.  *
  15.  * usage:  doc2ms < file.doc > file.ms
  16.  *
  17.  *   where file.doc is a VMS .DOC file, and file.ms will be a [nt]roff
  18.  *     document suitable for printing with nroff -ms or troff -ms
  19.  *
  20.  * typical usage for GNUPLOT:
  21.  *
  22.  *   doc2ms < gnuplot.doc | troff -ms
  23.  */
  24.  
  25. static char rcsid[] = "$Id: doc2ms.c,v 3.26 1992/03/25 04:53:29 woo Exp woo $";
  26.  
  27. #include <stdio.h>
  28. #include <ctype.h>
  29. #ifdef AMIGA_LC_5_1
  30. #include <string.h>
  31. #endif
  32.  
  33. #define MAX_NAME_LEN    256
  34. #define MAX_LINE_LEN    256
  35. #define LINE_SKIP        3
  36.  
  37. #define TRUE 1
  38. #define FALSE 0
  39.  
  40. typedef int boolean;
  41.  
  42. static boolean intable = FALSE;
  43.  
  44. main()
  45. {
  46.     init(stdout);
  47.     convert(stdin,stdout);
  48.     finish(stdout);
  49.     exit(0);
  50. }
  51.  
  52.  
  53. init(b)
  54. FILE *b;
  55. {
  56.     /* in nroff, increase line length by 8 and don't adjust lines */
  57.     (void) fputs(".if n \\{.nr LL +8m\n.na \\}\n",b);
  58.     (void) fputs(".nr PO +0.3i\n",b);
  59.     (void) fputs(".so titlepage.ms\n",b);
  60.     (void) fputs(".pn 1\n",b);
  61.     (void) fputs(".bp\n",b);
  62.     (void) fputs(".ta 1.5i 3.0i 4.5i 6.0i 7.5i\n",b);
  63.     (void) fputs("\\&\n.sp 3\n.PP\n",b);
  64.     /* following line commented out by rjl
  65.       (void) fputs(".so intro\n",b);
  66.       */
  67. }
  68.  
  69.  
  70. convert(a,b)
  71.     FILE *a,*b;
  72. {
  73.     static char line[MAX_LINE_LEN];
  74.  
  75.     while (fgets(line,MAX_LINE_LEN,a)) {
  76.        process_line(line, b);
  77.     }
  78. }
  79.  
  80. process_line(line, b)
  81.     char *line;
  82.     FILE *b;
  83. {
  84.     switch(line[0]) {        /* control character */
  85.        case '?': {            /* interactive help entry */
  86.           break;            /* ignore */
  87.        }
  88.        case '@': {            /* start/end table */
  89.           if (intable) {
  90.              (void) fputs(".TE\n.KE\n", b);
  91.              (void) fputs(".EQ\ndelim off\n.EN\n\n",b);
  92.              intable = FALSE;
  93.           } else {
  94.              (void) fputs("\n.EQ\ndelim $$\n.EN\n",b);
  95.              (void) fputs(".KS\n.TS\ncenter box tab (@) ;\n", b);
  96.              (void) fputs("c c l .\n", b);
  97.              intable = TRUE;
  98.           }
  99.           /* ignore rest of line */
  100.           break;
  101.        }
  102.        case '#': {            /* latex table entry */
  103.           break;            /* ignore */
  104.        }
  105.        case '%': {            /* troff table entry */
  106.           if (intable)
  107.             (void) fputs(line+1, b); /* copy directly */
  108.           else
  109.             fprintf(stderr, "error: % line found outside of table\n");
  110.           break;
  111.        }
  112.        case '\n':            /* empty text line */
  113.        case ' ': {            /* normal text line */
  114.           if (intable)
  115.             break;        /* ignore while in table */
  116.           switch(line[1]) {
  117.              case ' ': {
  118.                 /* verbatim mode */
  119.                 fputs(".br\n",b); 
  120.                 fputs(line+1,b); 
  121.                 fputs(".br\n",b);
  122.                 break;
  123.              }
  124.              case '\'': {
  125.                 fputs("\\&",b);
  126.                 putms(line+1,b); 
  127.                 break;
  128.              }
  129.              default: {
  130.                 if (line[0] == '\n')
  131.                   putms(line,b); /* handle totally blank line */
  132.                 else
  133.                   putms(line+1,b);
  134.                 break;
  135.              }
  136.              break;
  137.           }
  138.           break;
  139.        }
  140.        default: {
  141.           if (isdigit(line[0])) { /* start of section */
  142.              if (!intable)    /* ignore while in table */
  143.                section(line, b);
  144.           } else
  145.             fprintf(stderr, "unknown control code '%c' in column 1\n", 
  146.                   line[0]);
  147.           break;
  148.        }
  149.     }
  150. }
  151.  
  152.  
  153. /* process a line with a digit control char */
  154. /* starts a new [sub]section */
  155.  
  156. section(line, b)
  157.     char *line;
  158.     FILE *b;
  159. {
  160.     static char string[MAX_LINE_LEN];
  161.     int sh_i;
  162.     static int old = 1;
  163.  
  164.   
  165. #ifdef AMIGA_LC_5_1
  166.     (void) sscanf(line,"%d",&sh_i);
  167.     strcpy(string,strchr(line,' ')+1);
  168.     {
  169.       char *p;
  170.       p = strchr(string,'\n');
  171.       if (p != NULL) *p = '\0';
  172.     }
  173. #else
  174.     (void) sscanf(line,"%d %[^\n]s",&sh_i,string);
  175. #endif
  176.     
  177.     (void) fprintf(b,".sp %d\n",(sh_i == 1) ? LINE_SKIP : LINE_SKIP-1);
  178.     
  179.     if (sh_i > old) {
  180.        do
  181.         if (old!=1)    /* this line added by rjl */
  182.           (void) fputs(".RS\n.IP\n",b);
  183.        while (++old < sh_i);
  184.     }
  185.     else if (sh_i < old) {
  186.        do
  187.                if (sh_i!=1) /* this line added by rjl */
  188.                 (void) fputs(".RE\n.br\n",b);
  189.        while (--old > sh_i);
  190.     }
  191.     
  192.     /* added by dfk to capitalize section headers */
  193.     if (islower(string[0]))
  194.      string[0] = toupper(string[0]);
  195.     
  196.     /* next 3 lines added by rjl */
  197.     if (sh_i!=1) 
  198.      (void) fprintf(b,".NH %d\n%s\n.sp 1\n.LP\n",sh_i-1,string);
  199.     else 
  200.      (void) fprintf(b,".NH %d\n%s\n.sp 1\n.LP\n",sh_i,string);
  201.     old = sh_i;
  202.     
  203.     (void) fputs(".XS\n",b);
  204.     (void) fputs(string,b);
  205.     (void) fputs("\n.XE\n",b);
  206. }
  207.  
  208. putms(s, file)
  209.     char *s;
  210.     FILE *file;
  211. {
  212.     static boolean inquote = FALSE;
  213.  
  214.     while (*s != '\0') {
  215.        switch (*s) {
  216.           case '`': {        /* backquote -> boldface */
  217.              if (inquote) {
  218.                 fputs("\\fR", file);
  219.                 inquote = FALSE;
  220.              } else {
  221.                 fputs("\\fB", file);
  222.                 inquote = TRUE;
  223.              }
  224.              break;
  225.           }
  226.           case '\\': {        /* backslash */
  227.              fputs("\\\\", file);
  228.              break;
  229.           }
  230.           default: {
  231.              fputc(*s, file);
  232.              break;
  233.           }
  234.        }
  235.        s++;
  236.     }
  237. }
  238.  
  239. finish(b)        /* spit out table of contents */
  240. FILE *b;
  241. {
  242.     (void) fputs(".pn 1\n",b);
  243.     (void) fputs(".ds RH %\n",b);
  244.     (void) fputs(".af % i\n",b);
  245.     (void) fputs(".bp\n.PX\n",b);
  246. }
  247.